package com.framework.core.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间工具类
 */
public class DateUtils {
	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
    /**
     * 默认格式
     */
    public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String TIME_FORMAT = "HH:mm:ss";

    private static final SimpleDateFormat UTC_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    static {
        UTC_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    /**
     * format date to utc string
     *
     * @param date
     * @return
     */
    public static String formUTCDate2Str(Date date) {
        return UTC_DATE_FORMAT.format(date);
    }

    /**
     * format utc time string
     *
     * @param utcStr
     * @return
     * @throws ParseException
     */
    public static String formUTCTime(String utcStr) throws ParseException {
        Date utc = UTC_DATE_FORMAT.parse(utcStr);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
        return sdf.format(utc);
    }

    /**
     * format utc time long
     *
     * @param utcStr
     * @return
     * @throws ParseException
     */
    public static long formUTCTime2Long(String utcStr) throws ParseException {
        Date date = UTC_DATE_FORMAT.parse(utcStr);
        return date.getTime();
    }

    /**
     * 字符串转Date
     *
     * @param dateStr
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date getDateByString(String dateStr, String format) throws ParseException {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        format = StringUtils.isNotEmpty(format) ? format : FORMAT;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(dateStr);
    }

    /**
     * 字符串转Date
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date getDateByString(String dateStr) throws ParseException {
        return getDateByString(dateStr, FORMAT);
    }

    // 查询开始时间
    public static String tranStartTime2(String date) {
        try {
            Date startTime = new Date(Long.valueOf(date));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = sdf.format(startTime);
            return dateString;
        } catch (Exception e) {
            logger.error("tranStartTime2 error", e);
            return null;
        }
    }

    // 查询结束时间
    public static String tranEndTime2(String date) {
        try {
            Date endTime = new Date(Long.valueOf(date));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = sdf.format(endTime);
            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(dateString));
            cd.add(Calendar.DATE, 1);// 增加一天
            return sdf.format(cd.getTime());
        } catch (ParseException e) {
            logger.error("tranEndTime2 error", e);
            return null;
        }
    }

    /**
     * 获取之后的日期
     *
     * @param startDate
     * @param num
     * @param type      类型
     * @return
     */
    public static Date getAfterDate(Date startDate, int num, int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(type, num);
        return calendar.getTime();
    }

    /**
     * 获取当前时间
     */
    public static String getDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT);
        return formatter.format(date);
    }

    /**
     * 获取两个时间相差的时间
     *
     * @param startDate
     * @param endDate
     * @param type      类型:Calendar.MONTH,Calendar.DATE,,Calendar.HOUR
     * @return
     */
    public static long getCompareDate(Date startDate, Date endDate, int type) {
        long nh = 60 * 60;//一小时的秒数
        long nd = 24 * 60 * 60;//一天的秒数
        long nm = 30 * 24 * 60 * 60;//一个月的秒数
        long date = 0;//计算差多少时间
        long diff = 0;
        try {
            diff = endDate.getTime() - startDate.getTime();//获得两个时间的毫秒时间差异
        } catch (Exception e) {
            logger.error("getCompareDate error", e);
        }
        diff = diff / 1000;
        if (type == Calendar.MONTH) {
            //相差 n月
            date = diff / nm;
        } else if (type == Calendar.DATE) {
            //相差 n天
            date = diff / nd;
        } else if (type == Calendar.HOUR) {
            //相差 n小时
            date = diff / nh;
        } else if (type == Calendar.MINUTE) {
            //相差 n分钟
            date = diff / 60;
        } else if (type == Calendar.SECOND) {
            //相差 n分钟
            date = diff;
        }
        return date;
    }

    /**
     * 分钟转化为天/小时/分钟 格式
     *
     * @param min
     * @return
     */
    public static String minConvertDayHourMin(Integer min) {
        String html = "0分";
        if (min != null) {
            String format;
            Object[] array;
            Integer days = (int) (min / (60 * 24));
            Integer hours = (int) (min / (60) - days * 24);
            Integer minutes = (int) (min - hours * 60 - days * 24 * 60);
            if (days > 0) {
                format = "%1$,d天%2$,d时%3$,d分";
                array = new Object[]{days, hours, minutes};
            } else if (hours > 0) {
                format = "%1$,d时%2$,d分";
                array = new Object[]{hours, minutes};
            } else {
                format = "%1$,d分";
                array = new Object[]{minutes};
            }
            html = String.format(format, array);
        }
        return html;
    }

    /**
     * 2个日期比较大小
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(Date date1, Date date2) {
        return date1.getTime() >= date2.getTime();
    }

    /**
     * yyyy年MM月dd日
     *
     * @param date
     * @return
     */
    public static String DateToYmd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String str = sdf.format(date);
        return str;
    }

    /**
     * "yyyy年MM月dd日 HH时mm分ss秒
     *
     * @param date
     * @return
     */
    public static String DateToYmdHms(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 返回几小时后的某个时间
     *
     * @param d
     * @param hour
     * @return
     */
    public static String getDateAfterHour(Date d, int hour) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
        return getDate(now.getTime());
    }

    /**
     * 获取当前日期的前后时间
     *
     * @param startDate
     * @param num
     * @param type      类型:Calendar.MONTH,Calendar.DATE
     * @return
     */
    public static String getAroundDate(Date startDate, int num, int type, String dformat) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(type, num);
        return getDate(dformat, calendar.getTime());

    }

    public static String getDate(String format, Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 获取两个时间相差的天数，小于一天算0
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getCompareDate(String startDate, String endDate) {
        SimpleDateFormat sformat = new SimpleDateFormat(FORMAT);
        Date begintime = null;
        Date endtime = null;
        try {
            begintime = sformat.parse(startDate);
            endtime = sformat.parse(endDate);
        } catch (ParseException e) {
            logger.error("getCompareDate error", e);
        }
        long day = (endtime.getTime() - begintime.getTime()) / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
     * 获取两个时间相差的天数,小于一天算一天
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getCompareDateMore(String startDate, String endDate) {
        SimpleDateFormat sformat = new SimpleDateFormat(FORMAT);
        Date begintime = null;
        Date endtime = null;
        try {
            begintime = sformat.parse(startDate);
            endtime = sformat.parse(endDate);
        } catch (ParseException e) {
            logger.error("getCompareDateMore error", e);
        }
        long day = ((endtime.getTime() - begintime.getTime()) % (24 * 60 * 60 * 1000) == 0 ?
                (endtime.getTime() - begintime.getTime()) / (24 * 60 * 60 * 1000) :
                (endtime.getTime() - begintime.getTime()) / (24 * 60 * 60 * 1000) + 1);
        return day;
    }

    /**
     * 获取两个时间相差的小时，小于一小时算0
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getCompareHour(String startDate, String endDate) {
        SimpleDateFormat sformat = new SimpleDateFormat(FORMAT);
        Date begintime = null;
        Date endtime = null;
        try {
            begintime = sformat.parse(startDate);
            endtime = sformat.parse(endDate);
        } catch (ParseException e) {
            logger.error("getCompareDate error", e);
        }
        long hour = (endtime.getTime() - begintime.getTime()) / (60 * 60 * 1000);
        return hour;
    }

    /**
     * 时间格式化
     *
     * @param longTime
     * @param isStart  true-00:00:00,false-23:59:59
     * @return
     */
    public static Date getStartOrEndDate(String longTime, boolean isStart) {
        if (StringUtils.isEmpty(longTime) || !StringUtils.isNumeric(longTime))
            return null;

        Date date = new Date(Long.valueOf(longTime));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (isStart) {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        } else {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        }
        return calendar.getTime();
    }

  /*  public static void main(String[] args){
        try{
            Date startDate = getDateByString("2017-03-17 00:00:00");
            System.out.println(getAfterDate(startDate, 360, Calendar.DATE));
        }catch(Exception e){
            e.printStackTrace();
        }
    }*/
}
